﻿using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using EzDesk.Apps.Model.ProjectModule;
using EzDesk.Utility.ServiceStatusInfo;

namespace EzDesk.Clients.DesktopWeb.Controllers
{
    public partial class ProjectController
    {
        #region Page

        public ActionResult ProjectList()
        {
            dynamic model = new ExpandoObject();
            var projectModelList = new List<dynamic>();
            var projectList = ProjectService.GetProjectsForUser(CurrentUserId).Result;

            foreach (var project in projectList)
            {
                var memberList = project.MemberInProject.Select(
                    p =>
                    {
                        var roleStr = "member";
                        if (p.UserId == project.ChargePersonId) roleStr = "leader";
                        return new
                            {
                                Member = EzApiClient.GetSimpleUserById(p.UserId),
                                Role = roleStr
                            };
                    }).ToList();
                var leader = memberList.FirstOrDefault(p => p.Member.Id == project.ChargePersonId);

                var projectModel = new
                {
                    Id = project.Id,
                    Name = project.Name,
                    ProjectId = project.Code,
                    Explanation = project.Description,
                    CreatOn = project.CreateOn,
                    Leader = leader,
                    MemberList = memberList,
                };
                projectModelList.Add(projectModel);
            }

            model.ProjectList = projectModelList;
            return View(model);
        }
        public ActionResult ProjectMembers(Guid id)
        {
            //todo:
            dynamic model = new ExpandoObject();
            var project = ProjectService.GetProjectById(id).Result;

            var memberList = project.MemberInProject.Select(
                p =>
                {
                    var roleStr = "member";
                    if (p.UserId == project.ChargePersonId) roleStr = "leader";
                    return new
                    {
                        Member = EzApiClient.GetSimpleUserById(p.UserId),
                        Role = roleStr
                    };
                }).ToList();
            var leader = memberList.FirstOrDefault(p => p.Member.Id == project.ChargePersonId);

            var projectModel = new
            {
                Id = project.Id,
                Name = project.Name,
                Code = project.Code,
                Explanation = project.Description,
                CreateOn = project.CreateOn,
                Leader = leader,
                MemberList = memberList,
            };

            model.ProjectInfo = projectModel;
            return View(model);
        }
        public ActionResult ProjectWorkitems(Guid id)
        {
            dynamic model = new ExpandoObject();
            var project = ProjectService.GetProjectById(id).Result;
            model.ProjectInfo = new
            {
                Id = project.Id,
                Name = project.Name,
                Code = project.Code,
                CreateOn = project.CreateOn,
                Explanation = project.Description
            };

            return View(model);
        }

        #endregion Page

        #region Ajax
        [HttpPost]
        public ActionResult DoUpdateProjectMemberString(Guid projectId, string memberString)
        {
            var statusInfo = ProjectService.UpdateProjectMember(projectId, memberString);
            if (statusInfo.Status == ServiceStatus.Success)
            {
                statusInfo.Info = "更新项目成员成功。";
            }
            return Json(statusInfo);
        }

        [HttpPost]
        public ActionResult DoUpdateProjectDescription(Guid projectId, string description)
        {
            var statusInfo = ProjectService.UpdateDescription(projectId, description);
            if (statusInfo.Status == ServiceStatus.Success)
            {
                statusInfo.Info = "更新项目备注完成。";
            }
            return Json(statusInfo);
        }

        [HttpPost]
        public ActionResult DoCreateProject(string projectName, string projectCode, Guid chargeUserId)
        {
            var project = new Project()
            {
                Name = projectName,
                Code = projectCode,
                Description = "",
                ChargePersonId = chargeUserId,
                MemberString = chargeUserId.ToString()
            };

            var statusInfo = ProjectService.CreateNewProject(project);
            if (statusInfo.Status == ServiceStatus.Success)
            {
                statusInfo.Info = "创建项目成功";
                statusInfo.Data = new { projectId = statusInfo.Result.Id.ToString() };
            }
            return Json(statusInfo);
        }

        public JsonResult GetProjectListForUser()
        {
            var projectList = ProjectService.GetProjectsForUser(CurrentUserId).Result;
            var projectListModel = projectList.Select(
                p => new
                    {
                        Name = p.Name,
                        Id = p.Id
                    }
            ).ToList();

            return Json(
               projectListModel,
               JsonRequestBehavior.AllowGet
               );
        }

        public JsonResult GetWorkitemsInProject(Guid projectId)
        {
            var workitemList = ProjectService.GetWorkitems(null, null, projectId, true, null, null, null, null, null)
                .Result
                .OrderByDescending(workitem => workitem.CreateOn);

            foreach (var workitem in workitemList)
            {
                workitem.OwnerName = EzApiClient.GetSimpleUserById(workitem.OwnerId).Name;
                workitem.CreatorName = EzApiClient.GetSimpleUserById(workitem.CreatorId).Name;
            }

            return Json(workitemList, JsonRequestBehavior.AllowGet);
        }

        public JsonResult GetUnfinishedWorkitemsInProject(Guid projectId)
        {
            var workitemList = ProjectService.GetWorkitems(null, null, projectId, false, null, null, null, null, null)
                .Result
                .OrderByDescending(workitem => workitem.CreateOn);

            foreach (var workitem in workitemList)
            {
                workitem.OwnerName = EzApiClient.GetSimpleUserById(workitem.OwnerId).Name;
                workitem.CreatorName = EzApiClient.GetSimpleUserById(workitem.CreatorId).Name;
            }

            return Json(workitemList, JsonRequestBehavior.AllowGet);
        }

        #endregion Ajax
    }
}
